package com.t249.bdqn.renting.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.t249.bdqn.renting.dao.*;
import com.t249.bdqn.renting.entity.RetImg;
import com.t249.bdqn.renting.entity.RetUser;
import com.t249.bdqn.renting.util.TxCloudSmsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/wechatapplet")
public class WechatappletController {

    @Resource
    private RetAddressDao retAddressdao;

    @Resource
    private RetRegionDao retRegionDao;

    @Resource
    private RetImgDao retImgDao;

    @Resource
    private RetRentalInformationDao retRentalInformationDao;

    @Resource
    private RetPropertiesDao retPropertiesDao;

    @Resource
    private TxCloudSmsUtil txCloudSmsUtil;

    @Resource
    private RetUserDao retUserDao;

    @Resource
    private SysBookingDao sysBookingDao;

    @Resource
    private RetCollectionDao retCollectionDao;


    //根据id(城市id)显示相关的房源信息
    @RequestMapping(value = "/shuju")
    @ResponseBody
    public String shuju(){
        //没有id就默认为显示深圳的房源
        List<Map<String,Object>> retRentalInformationList = retRentalInformationDao.ChengshiSelect(1);
        return JSONArray.toJSONString(retRentalInformationList);
    }

    //根据点击的房源id显示点击的房源详情
    @RequestMapping(value = "/suoyou")
    @ResponseBody
    public String suoyou(Integer id){
        //根据点击房源传过来的id来查询相对应的房源基本信息
        List<Map<String,Object>> retRentalInformationList = retRentalInformationDao.FanyuanSelect(id);
        return JSONArray.toJSONString(retRentalInformationList);
    }

    //根据点击的房源id显示点击的房源图片
    @RequestMapping(value = "/img")
    @ResponseBody
    public String img(Integer id){
        //根据点击房源传过来的id来查询相对应的房源图片
        List<RetImg> retImgList = retImgDao.selectList(new QueryWrapper<RetImg>().eq("ret_rental_Id",id));
        return JSONArray.toJSONString(retImgList);
    }

    //手机号绑定
    @RequestMapping(value = "/userBandin",method = RequestMethod.POST)
    @ResponseBody
    public String userBandin(RetUser retUser){
        String fanhui = null;
        int count = retUserDao.selectCount(new QueryWrapper<RetUser>().eq("phone",retUser.getPhone()));
        if(count > 0){
            RetUser retUser1 = retUserDao.selectOne(new QueryWrapper<RetUser>().eq("phone",retUser.getPhone()));
            retUser.setId(retUser1.getId());
            int inser = retUserDao.updateById(retUser);
            if(inser > 0){
                fanhui = retUser1.getPhone();
            }else{
                fanhui = "1";
            }
        }else{
            int zhi = retUserDao.insert(retUser);
            if(zhi > 0){
                int maxid = retUserDao.maxId();
                retUser.setId(maxid);
                int inser = retUserDao.updateById(retUser);
                if(inser > 0){
                    fanhui = retUser.getPhone();
                }else{
                    fanhui = "1";
                }
            }
        }
        return  JSONArray.toJSONString(fanhui);
    }

    //根据手机号码显示相关的信息
    @RequestMapping(value = "/selectPhone",method = RequestMethod.POST)
    @ResponseBody
    public String selectPhone(String phone){
        List<RetUser> retUserList = null;
        if(phone != null){
            retUserList = retUserDao.selectList(new QueryWrapper<RetUser>().eq("phone",phone));
            return JSONArray.toJSONString(retUserList);
        }else {
            return JSONArray.toJSONString("1");
        }
    }
}
